﻿#! /bin/sh
(
#--------------------------------------------------------------------------------
# Init
#--------------------------------------------------------------------------------
BUILD="yes"
DEST_LANG="en_US.UTF-8"
TZDATA="Europe/Paris"
HOST="SERVER"
ROOTPWD="4365" 
CPUS=$(grep -c 'processor' /proc/cpuinfo)
CTHREADS="-j$(($CPUS + $CPUS/2))"
DEST=$(pwd)/output
SRC=$(pwd)
VERSION="DebianJessie 3.14.14-cubox armv7l"
VERSION="${VERSION// /_}"

export DEBIAN_FRONTEND=noninteractive
start=`date +%s`
#--------------------------------------------------------------------------------
# Dependence
#--------------------------------------------------------------------------------
echo 'deb http://www.emdebian.org/debian unstable main' > /etc/apt/sources.list.d/emdebian.list
apt-get update
apt-get --yes --force-yes install emdebian-archive-keyring debootstrap qemu-user-static gcc-4.7-arm-linux-gnueabihf git u-boot-tools lzop make gcc hdparm dosfstools bc parted zip
ln -s /usr/bin/arm-linux-gnueabihf-gcc-4.7 /usr/bin/arm-linux-gnueabihf-gcc
#--------------------------------------------------------------------------------
# Download
#--------------------------------------------------------------------------------
mkdir -p $DEST/output
wget -q http://www.tremende.com/ramlog/download/ramlog_2.0.0_all.deb -O $DEST/ramlog_2.0.0_all.deb
if [ -d "$DEST/u-boot-cubox" ]; then
	cd $DEST/u-boot-cubox ; git pull; cd $SRC
else
	git clone https://github.com/SolidRun/u-boot-imx6 $DEST/u-boot-cubox 
fi
if [ -d "$DEST/linux-imx6-3.14" ]; then
	cd $DEST/linux-imx6-3.14; git pull -f; cd $SRC
else
	git clone https://github.com/SolidRun/linux-imx6-3.14.git $DEST/linux-imx6-3.14
fi
#--------------------------------------------------------------------------------
# Build
#--------------------------------------------------------------------------------
if [ "$BUILD" = "yes" ]; then
	rm -rf $DEST/linux-cubox/output
	cd $DEST/u-boot-cubox
	make CROSS_COMPILE=arm-linux-gnueabihf- clean
	make $CTHREADS mx6_cubox-i_config CROSS_COMPILE=arm-linux-gnueabihf- 
	make $CTHREADS CROSS_COMPILE=arm-linux-gnueabihf-
	rm -rf $DEST/linux-imx6-3.14/output
	cd $DEST/linux-imx6-3.14
	make CROSS_COMPILE=arm-linux-gnueabihf- clean
	make ARCH=arm imx_v7_cbi_hb_base_defconfig
	make $CTHREADS ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules imx6q-cubox-i.dtb imx6dl-cubox-i.dtb imx6dl-hummingboard.dtb imx6q-hummingboard.dtb
	make $CTHREADS ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=output modules_install
	make $CTHREADS ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_HDR_PATH=output/usr headers_install
	cp $DEST/linux-imx6-3.14/Module.symvers $DEST/linux-imx6-3.14/output/usr/include
fi
#--------------------------------------------------------------------------------
# Prepare
#--------------------------------------------------------------------------------
rm -rf $DEST/output/boot/
mkdir -p $DEST/output/boot/
echo 'mmcargs=setenv bootargs root=/dev/mmcblk0p1 rootfstype=ext4 rootwait console=ttymxc0,115200n8 video=mxcfb0:dev=hdmi,1920x1080M@60,if=RGB24,bpp=32 rd.dm=0 rd.luks=0 rd.lvm=0 raid=noautodetect quiet' > $DEST/output/boot/uEnv.txt
cp $DEST/u-boot-cubox/u-boot.img $DEST/output/u-boot.img
cp $DEST/u-boot-cubox/SPL $DEST/output/SPL
cp $DEST/linux-imx6-3.14/arch/arm/boot/zImage $DEST/output/boot/zImage
cp $DEST/linux-imx6-3.14/arch/arm/boot/dts/imx6*.dtb $DEST/output/boot/
############ ??? ############
VER=$(cat $DEST/linux-imx6-3.14/Makefile | grep VERSION | head -1 | awk '{print $(NF)}')
VER=$VER.$(cat $DEST/linux-imx6-3.14/Makefile | grep PATCHLEVEL | head -1 | awk '{print $(NF)}')
VER=$VER.$(cat $DEST/linux-imx6-3.14/Makefile | grep SUBLEVEL | head -1 | awk '{print $(NF)}')
cd $DEST/linux-imx6-3.14/output
rm -f $DEST"/output/cubox_kernel_"$VER"_mod_head_fw.tar"
tar cPf $DEST"/output/cubox_kernel_"$VER"_mod_head_fw.tar" *
cd $DEST/output/
tar rPf $DEST"/output/cubox_kernel_"$VER"_mod_head_fw.tar" boot/*
md5sum cubox_kernel_"$VER"_mod_head_fw.tar > cubox_kernel_"$VER"_mod_head_fw.md5
zip cubox_kernel_"$VER"_mod_head_fw.zip cubox_kernel_"$VER"_mod_head_fw.*
############ ??? ############
cd $DEST/output
dd if=/dev/zero of=debian_rootfs.raw bs=1M count=1000 status=noxfer
sleep 3
LOOP=$(losetup -f)
losetup $LOOP debian_rootfs.raw
sync
parted -s $LOOP -- mklabel msdos
sleep 1
parted -s $LOOP -- mkpart primary ext4  2048s -1s
sleep 1
partprobe $LOOP
sleep 1
dd if=$DEST/output/SPL of=$LOOP bs=512 seek=2 status=noxfer
dd if=$DEST/output/u-boot.img of=$LOOP bs=1K seek=42 status=noxfer
rm $DEST/output/SPL
rm $DEST/output/u-boot.img
sync
sleep 5
losetup -d $LOOP
sleep 4
losetup -o 1048576 $LOOP debian_rootfs.raw
sleep 4
mkfs.ext4 $LOOP
tune2fs -o journal_data_writeback $LOOP
e2label $LOOP "Root"
mkdir -p $DEST/output/sdcard/
mount -t ext4 $LOOP $DEST/output/sdcard/
debootstrap --no-check-gpg --arch=armhf --foreign jessie $DEST/output/sdcard/
cp /usr/bin/qemu-arm-static $DEST/output/sdcard/usr/bin/
test -e /proc/sys/fs/binfmt_misc/qemu-arm || update-binfmts --enable qemu-arm
mount -t proc chproc $DEST/output/sdcard/proc
chroot $DEST/output/sdcard /bin/bash -c "/debootstrap/debootstrap --second-stage"
mount -t proc chproc $DEST/output/sdcard/proc
mount -t sysfs chsys $DEST/output/sdcard/sys
mount -t devtmpfs chdev $DEST/output/sdcard/dev || mount --bind /dev $DEST/output/sdcard/dev
mount -t devpts chpts $DEST/output/sdcard/dev/pts
#--------------------------------------------------------------------------------
# System
#--------------------------------------------------------------------------------
cat <<EOT >> $DEST/output/sdcard/etc/fstab
/dev/mmcblk0p1  /           ext4    defaults,noatime,nodiratime,data=writeback,commit=600,errors=remount-ro        0       0

EOT
cat <<EOT >> $DEST/output/sdcard/etc/network/interfaces
auto eth0
allow-hotplug eth0
iface eth0 inet dhcp
EOT
cat <<EOT >> $DEST/output/sdcard/etc/apt/sources.list
deb http://ftp.fr.debian.org/debian/ jessie main non-free contrib
deb-src http://ftp.fr.debian.org/debian/ jessie main non-free contrib
deb http://security.debian.org/ jessie/updates main contrib non-free
deb-src http://security.debian.org/ jessie/updates main contrib non-free
deb http://ftp.fr.debian.org/debian/ jessie-updates main contrib non-free
deb-src http://ftp.fr.debian.org/debian/ jessie-updates main contrib non-free
EOT
echo $HOST > $DEST/output/sdcard/etc/hostname
chroot $DEST/output/sdcard /bin/bash -c "(echo $ROOTPWD;echo $ROOTPWD;) | passwd root" 
chroot $DEST/output/sdcard /bin/bash -c "apt-get update"
chroot $DEST/output/sdcard /bin/bash -c "apt-get -qq -y install locales makedev"
sed -i "s/^# $DEST_LANG/$DEST_LANG/" $DEST/output/sdcard/etc/locale.gen
chroot $DEST/output/sdcard /bin/bash -c "locale-gen $DEST_LANG"
chroot $DEST/output/sdcard /bin/bash -c "export LANG=$DEST_LANG LANGUAGE=$DEST_LANG DEBIAN_FRONTEND=noninteractive"
chroot $DEST/output/sdcard /bin/bash -c "update-locale LANG=$DEST_LANG LANGUAGE=$DEST_LANG LC_MESSAGES=POSIX"
echo $TZDATA > $DEST/output/sdcard/etc/timezone
chroot $DEST/output/sdcard /bin/bash -c "dpkg-reconfigure -f noninteractive tzdata"
############ ??? ############
cd $DEST/output/sdcard/
ls ../*.tar | xargs -i tar xf {}
sleep 5
rm $DEST/output/*.md5
rm $DEST/output/*.tar
find $DEST/output/sdcard/lib/modules -type l -exec rm -f {} \;
############ ??? ############
#--------------------------------------------------------------------------------
# Application
#--------------------------------------------------------------------------------
chroot $DEST/output/sdcard /bin/bash -c "apt-get update"
chroot $DEST/output/sdcard /bin/bash -c "apt-get -qq -y install cpufrequtils openssh-server lsof rsync fake-hwclock ntp"
chroot $DEST/output/sdcard /bin/bash -c "apt-get -y clean"
cp $DEST/ramlog_2.0.0_all.deb $DEST/output/sdcard/tmp
chroot $DEST/output/sdcard /bin/bash -c "dpkg -i /tmp/ramlog_2.0.0_all.deb"
cp $SRC/firstrun.sh $DEST/output/sdcard/etc/init.d/firstrun
chroot $DEST/output/sdcard /bin/bash -c "chmod +x /etc/init.d/firstrun"
chroot $DEST/output/sdcard /bin/bash -c "update-rc.d firstrun defaults"
cp $SRC/bootsplash.sh $DEST/output/sdcard/etc/init.d/bootsplash
chroot $DEST/output/sdcard /bin/bash -c "chmod +x /etc/init.d/bootsplash"
chroot $DEST/output/sdcard /bin/bash -c "update-rc.d bootsplash defaults"
#--------------------------------------------------------------------------------
# Tuning
#--------------------------------------------------------------------------------
sed -e 's/#RAMTMP=no/RAMTMP=yes/g' -i $DEST/output/sdcard/etc/default/tmpfs
sed -e 's/#RUN_SIZE=10%/RUN_SIZE=128M/g' -i $DEST/output/sdcard/etc/default/tmpfs 
sed -e 's/#LOCK_SIZE=/LOCK_SIZE=/g' -i $DEST/output/sdcard/etc/default/tmpfs 
sed -e 's/#SHM_SIZE=/SHM_SIZE=128M/g' -i $DEST/output/sdcard/etc/default/tmpfs 
sed -e 's/#TMP_SIZE=/TMP_SIZE=1G/g' -i $DEST/output/sdcard/etc/default/tmpfs 
sed -e 's/TMPFS_RAMFS_SIZE=/TMPFS_RAMFS_SIZE=256m/g' -i $DEST/output/sdcard/etc/default/ramlog
sed -e 's/# Required-Start:    $remote_fs $time/# Required-Start:    $remote_fs $time ramlog/g' -i $DEST/output/sdcard/etc/init.d/rsyslog
sed -e 's/# Required-Stop:     umountnfs $time/# Required-Stop:     umountnfs $time ramlog/g' -i $DEST/output/sdcard/etc/init.d/rsyslog
sed -e 's/MIN_SPEED="0"/MIN_SPEED="792000"/g' -i $DEST/output/sdcard/etc/init.d/cpufrequtils
sed -e 's/MAX_SPEED="0"/MAX_SPEED="996000"/g' -i $DEST/output/sdcard/etc/init.d/cpufrequtils
sed -e 's/ondemand/interactive/g' -i $DEST/output/sdcard/etc/init.d/cpufrequtils
cat <<EOT >> $DEST/output/sdcard/etc/sysfs.conf
block/mmcblk0/queue/scheduler = noop
EOT
#--------------------------------------------------------------------------------
# Clean
#--------------------------------------------------------------------------------
umount -l $DEST/output/sdcard/dev/pts
umount -l $DEST/output/sdcard/dev
umount -l $DEST/output/sdcard/proc
umount -l $DEST/output/sdcard/sys
sleep 4
rm $DEST/output/sdcard/usr/bin/qemu-arm-static 
umount -l $DEST/output/sdcard/
sleep 4
losetup -d $LOOP
mv $DEST/output/debian_rootfs.raw $DEST/output/$VERSION.raw
end=`date +%s`
runtime=$((end-start))
echo "Runtime $runtime sec."
) 2>&1 | tee -a $0.log